package cn.ransj.animation.util;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Display;

/**
 * image provider
 * 
 * @author android
 * 
 */
public class ImageProvider {
	private static final String[] IMG_PROS = { Media.DISPLAY_NAME,//
			Media._ID };//
	private static final int INDEX_NAME = 0;
	private static final int INDEX_ID = 1;
	private static ImageProvider mInstace = new ImageProvider();
	private Activity mContext;
	private Cursor mCursor;
	private int mScreenWidth;
	private int mScreenHeight;
	private boolean mIsLoop;

	/**
	 * private constructor
	 */
	private ImageProvider() {
	}

	public static ImageProvider getInstance() {
		return mInstace;
	}

	public Bitmap initProvider(Activity context) {
		mContext = context;
		mCursor = getImagesCursor(mContext);
		Display dis = context.getWindowManager().getDefaultDisplay();
		mScreenWidth = dis.getWidth();
		mScreenHeight = dis.getHeight();
		return getBitmapFromCursor(mCursor, context.getContentResolver(),
				mScreenWidth, mScreenHeight);
	}

	public void setLoop(boolean loop) {
		mIsLoop = loop;
	}
	
	public Bitmap nextImg(){
		if(mCursor.moveToNext()){
			Log.w("TAG", "current : "+mCursor.getString(INDEX_NAME));
			return getBitmapFromCursor(mCursor, mContext.getContentResolver(), mScreenWidth, mScreenHeight);
		}
		Log.w("TAG", "no more img ");
		return null;
	}

	private static Bitmap getBitmapFromCursor(Cursor cursor,
			ContentResolver resolver, int outWidth, int outHeight) {
		Long id = cursor.getLong(INDEX_ID);
		Uri uri = Media.EXTERNAL_CONTENT_URI.buildUpon()
				.appendPath(Long.toString(id)).build();
		return getSmallImg(resolver, uri, outWidth, outHeight);
	}

	private static Cursor getImagesCursor(Activity context) {
		Cursor cursor = context.getContentResolver().query(
				Media.EXTERNAL_CONTENT_URI, IMG_PROS, null, null, null);
		cursor.moveToFirst();
		return cursor;
	}

	public static Bitmap getSmallImg(ContentResolver resolver, Uri uri,
			int outWidth, int outHeight) {
		try {
			// Decode image size
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(resolver.openInputStream(uri), null, o);
			// Find the correct scale value. It should be the power of 2.
			int width_tmp = o.outWidth, height_tmp = o.outHeight;
			System.out.println(width_tmp + "  " + height_tmp);
			Log.w("===", (width_tmp + "  " + height_tmp));

			int scale = 1;
			while (true) {
				if (width_tmp / 2 < outWidth && height_tmp / 2 < outHeight)
					break;
				width_tmp /= 2;
				height_tmp /= 2;
				scale++;

				Log.w("===", scale + "''" + width_tmp + "  " + height_tmp);
			}
			// Decode with inSampleSize
			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			return BitmapFactory.decodeStream(resolver.openInputStream(uri),
					null, o2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
